<?php
/*
+--------------------------------------------------------------------------
|   Web: http://www.enbac.com 
|   Started date : 30/06/2008
+---------------------------------------------------------------------------
|   > Script written by Nova
+---------------------------------------------------------------------------
*/

if (eregi(basename( __FILE__ ), $_SERVER['PHP_SELF'])) {
	print "<h1>Incorrect access</h1>You cannot access this file directly.";
	exit();
}

class ajax_shop_giaodich {
	function playme(){
		$code = Url::get('code');
		switch( $code ){
			case 'action_item_comment':
				$this->action_item_comment();
				break;
			case 'action_reply_item_comment':
				$this->action_reply_item_comment();
				break;
			case 'action_entry_comment':
				$this->action_entry_comment();
				break;
			case 'action_reply_entry_comment':
				$this->action_reply_entry_comment();
				break;
			case 'action_user_comment':
				$this->action_user_comment();
				break;
			case 'show_c_user_line':
				$this->show_user_comment_line();
				break;
			case 'action_pm':
				$this->action_pm();
				break;
			case 'view_more_o_trans':
				$this->view_more_o_trans();
				break;
			default:
				$this->home();
				break;
		}
	}
	
	function home(){
		global $display;
		die("Nothing to do...");
	}

	function action_item_comment(){
		$act_	 	= Url::get('act_');
		$comment_id = (int)Url::get('c_item_id',0);
		$content    = '';
		
		$json 		= "";
		
		if(!User::is_login()){
			$json = '({"msg":"no_login"})';
			echo $json;
			exit;
		}
		
		if(User::is_block() || !$comment_id || ($act_!='skip' && $act_!='reply')){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();			
		}
		
		$comment_row = DB::select('comment',"id=$comment_id");
		if(!$comment_row){ 	
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();	
		}
		
		//if($comment_row['sender_user_id'] == User::id() ||  ($act_=='reply' && $comment_row['status']==1)){//Không được trả lời cho chính mình! hoặc trả lời phản hồi đã đc trả lời rồi!
		if($comment_row['sender_user_id'] == User::id()){//Không được trả lời cho chính mình!
			$json = '({"msg":"success"})';
			echo $json;
			exit();	
		}
		
		$item_id  		= $comment_row['item_id'];
		$item 			= Item::get_item($item_id);
		$item_memcache 	= $item;
			
		if(!($item && $item['user_id'] == User::id())){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();			
		}
	
		if($act_=='reply' && $item["state"]==1 && !User::have_permit(ADMIN_ITEM)){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();	
		}
		
		$json = '({"msg":"success"';
		
		if($act_=='reply'){
			$content    = trim(AZLib::getParam('content'));
			if(!$content){
				$json = '({"msg":"short_content"})';
				echo $json;
				exit();	
			}
			
			if(AZLib::checkBadWord($content)){
				$json = '({"msg":"bad_word"})';
				echo $json;
				exit();
			}
			
			$content 			= preg_replace( "/\n/", "<br />", $content);
			$content 			= str_replace('<br /><br />','&nbsp;',$content);
			
			if($comment_row['parent_id']){
				$parent_id		= $comment_row['parent_id'];
				
				if($comment_row['sender_user_id'])
					$content = '@<a href="'.WEB_DIR.$comment_row['sender_user_name'].'" class="fast_reply_link" title="'.$comment_row['sender_user_name'].'">'.$comment_row['sender_user_name'].'</a>: '.$content;
				else
					$content = '<font color="#999">@<span style="text-decoration:underline">'.$comment_row['sender_user_name'].'</span> </font>: '.$content;
			}
			else{
				$parent_id		= $comment_row['id'];
			}
			
			$comment = array(
							'content'			=>$content,
							'item_id'			=>$item['id'],
							'time'				=>time(),
							'order_time'		=>time(),
							'post_ip'			=>AZLib::ip(),
							'parent_id'			=>$parent_id,
							'receiver_user_id'	=>$item['user_id'],
							'receiver_user_name'=>$item['user_name'],
							'display'			=>1,
							'status'			=>1//đã đọc, đã trả lời
							);
							
			$comment['sender_user_id']	=User::id();
			$comment['sender_user_name']=User::user_name();
			
			if($comment_row['sender_user_id']){
				$comment['replied_user_id']		=$comment_row['sender_user_id'];
				$comment['replied_user_name'] 	=$comment_row['sender_user_name'];
			}
			
			$id=DB::insert('comment',$comment);
				
			if($id){
				$re = DB::query("SELECT id FROM comment WHERE parent_id = $parent_id AND display = 1 ORDER BY id DESC LIMIT 3,1");				
				
				if($re){
					if($row = mysql_fetch_assoc($re)){
						if($row){
							DB::query("UPDATE comment SET display = 0 WHERE parent_id = $parent_id AND display = 1 AND id<={$row['id']}");										
						}
					}
				}
				
				if($comment_id == $parent_id){
					DB::query("UPDATE comment SET status = 1, have_child = have_child + 1, order_time = ".time()." WHERE id = $comment_id");
				}
				else{
					DB::query("UPDATE comment SET have_child = have_child + 1, order_time = ".time()." WHERE id = $parent_id");
					DB::query("UPDATE comment SET status = 1 WHERE id = $comment_id");
				}
				
				if($comment_row['sender_user_id']){
					DB::query('UPDATE user SET total_new_comment = total_new_comment + 1 WHERE id='.$comment_row['sender_user_id']);
					User::getUser($comment_row['sender_user_id'],0,1);
				}
				//Cập nhật đã đọc - trả lời cho feed
				DB::query("UPDATE feed SET status = 1 WHERE ref_id = $comment_id AND type=1");
			}
			
			$total_feedback = DB::count('comment','item_id="'.$item['id'].'"');
	
			
			DB::update_id('item',array('reply_count'=>$total_feedback),$item['id']);
			if(MEMCACHE_ON){
				$caheFile='fb_'.$item['id'];
				StaticCache::delCache($caheFile);
				$item_memcache['reply_count'] = $total_feedback;
				AZMemcache::do_put("item:{$item['id']}",$item_memcache);
			}
			
			//Cập nhật cron job
			//...
		}
		else{
			if($comment_row['status']==0){
				DB::query("UPDATE comment SET status = 1 WHERE id = $comment_id");
			}
			//Cập nhật đã đọc - trả lời cho feed
			DB::query("UPDATE feed SET status = 1 WHERE ref_id = $comment_id AND type=1");
		}
		
		DB::query('UPDATE user SET total_new_comment = total_new_comment - 1 WHERE id='.User::id().' AND total_new_comment>0');
		User::getUser(User::id(),0,1);
				
		$json.="})";
		echo $json;
		//exit;
		System::halt();
	}
	
	//trả lời cho comment trong Giao dịch
	function action_reply_item_comment(){
		$act_	 	= Url::get('act_');
		$comment_id = (int)Url::get('re_c_item_id',0);
		$content    = '';
		
		$json 		= "";
		
		if(!User::is_login()){
			$json = '({"msg":"no_login"})';
			echo $json;
			exit;
		}
		
		
		if(User::is_block() || !$comment_id || ($act_!='skip' && $act_!='reply')){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();			
		}
		
		$comment_row = DB::select('comment',"id=$comment_id");
		if(!$comment_row || ($comment_row && $comment_row['replied_user_id'] != User::id())){ 	
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();	
		}
		
		//if($comment_row['receiver_user_id'] == User::id() || $comment_row['sender_user_id'] == User::id() ||  ($act_=='reply' && $comment_row['replied_status']==1)){//Không được trả lời cho chính mình! hoặc trả lời phản hồi đã đc trả lời rồi!
		if($comment_row['receiver_user_id'] == User::id() || $comment_row['sender_user_id'] == User::id()){//Không được trả lời cho chính mình!
			$json = '({"msg":"success"})';
			echo $json;
			exit();	
		}
		
		$item_id  		= $comment_row['item_id'];
		$item 			= Item::get_item($item_id);
		$item_memcache 	= $item;
		
		if(!$item || ($item && $item==-1)){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();			
		}
	
		if($item["state"]==1 && !User::have_permit(ADMIN_ITEM)){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();	
		}
		
		$json = '({"msg":"success"';
		
		if($act_=='reply'){
			
			$content    = trim(AZLib::getParam('content'));
			if(!$content){
				$json = '({"msg":"short_content"})';
				echo $json;
				exit();	
			}
			if(AZLib::checkBadWord($content)){
				$json = '({"msg":"bad_word"})';
				echo $json;
				exit();
			}
			
			$content 		= preg_replace( "/\n/", "<br />", $content);
			$content 		= str_replace('<br /><br />','&nbsp;',$content);
			
			if($comment_row['parent_id']){
				$parent_id		= $comment_row['parent_id'];
				
				if($comment_row['sender_user_id'])
					$content = '@<a href="'.WEB_DIR.$comment_row['sender_user_name'].'" class="fast_reply_link" title="'.$comment_row['sender_user_name'].'">'.$comment_row['sender_user_name'].'</a>: '.$content;
				else
					$content = '<font color="#999">@<span style="text-decoration:underline">'.$comment_row['sender_user_name'].'</span> </font>: '.$content;
			}
			else{
				$parent_id		= $comment_row['id'];
			}
			
			$comment = array(
							'content'			=>$content,
							'item_id'			=>$item['id'],
							'time'				=>time(),
							'order_time'		=>time(),
							'post_ip'			=>AZLib::ip(),
							'parent_id'			=>$parent_id,
							'receiver_user_id'	=>$item['user_id'],
							'receiver_user_name'=>$item['user_name'],
							'display'			=>1,
							'status'			=>0
							);
							
			$comment['sender_user_id']	=User::id();
			$comment['sender_user_name']=User::user_name();
			
			if($comment_row['sender_user_id']){
				$comment['replied_user_id']		=$comment_row['sender_user_id'];
				$comment['replied_user_name'] 	=$comment_row['sender_user_name'];
			}
			if($comment_row['sender_user_id'] == $item['user_id']){
				$comment['replied_status'] = 1;
			}
			
			$id=DB::insert('comment',$comment);
				
			if($id){
				$re = DB::query("SELECT id FROM comment WHERE parent_id = $parent_id AND display = 1 ORDER BY id DESC LIMIT 3,1");				
				
				if($re){
					if($row = mysql_fetch_assoc($re)){
						if($row){
							DB::query("UPDATE comment SET display = 0 WHERE parent_id = $parent_id AND display = 1 AND id<={$row['id']}");										
						}
					}
				}
				
				if($comment_id == $parent_id){
					DB::query("UPDATE comment SET replied_status = 1,have_child = have_child + 1, order_time = ".time()." WHERE id = $comment_id");
				}
				else{
					DB::query("UPDATE comment SET have_child = have_child + 1, order_time = ".time()." WHERE id = $parent_id");
					DB::query("UPDATE comment SET replied_status = 1 WHERE id = $comment_id");
				}
				//Cập nhật đã đọc - trả lời cho feed
				DB::query("UPDATE feed SET status=1 WHERE ref_id = $comment_id AND type=2");
				
				DB::query('UPDATE user SET total_new_comment = total_new_comment + 1 WHERE id='.$item['user_id']);
				User::getUser($item['user_id'],0,1);
				
				if($comment_row['sender_user_id'] && $comment_row['sender_user_id']!=$item['user_id']){
					DB::query('UPDATE user SET total_new_comment = total_new_comment + 1 WHERE id='.$comment_row['sender_user_id']);
					User::getUser($comment_row['sender_user_id'],0,1);
				}
			}
			
			$total_feedback = DB::count('comment','item_id="'.$item['id'].'"');
			DB::update_id('item',array('reply_count'=>$total_feedback),$item['id']);
			if(MEMCACHE_ON){
				$item_memcache['reply_count'] = $total_feedback;
				AZMemcache::do_put("item:{$item['id']}",$item_memcache);
			}
			
			$receiver_user = User::getUser($item['user_id'],1);
			
			//Cập nhật cron job
			if($receiver_user && $receiver_user['email'] && $receiver_user['email_alert']){								
				$link = WEB_ROOT.AZRewrite::formatUrl('?page=item_detail&id='.$item['id'].'&ebname='.AZLib::safe_title($item['name']));
				$link = AZRewrite::formatUrl($link);
				$title = "<a href='$link' target='_blank' style='text-decoration:none;color:#003399;'><font color='#003399'>{$item['name']}</font></a>";
					  
				AZLib::addCronJob('item_comment',AZLib::parseBBCode($content,true),$receiver_user['id'],User::user_name(),'',$item['id'],$title,$link);
			}
		}
		else{
			if($comment_row['replied_status']==0){
				DB::query("UPDATE comment SET replied_status = 1 WHERE id = $comment_id");
			}
			//Cập nhật đã đọc - trả lời cho feed
			DB::query("UPDATE feed set status=1 WHERE ref_id = $comment_id AND type=2");
		}
		
		DB::query('UPDATE user SET total_new_comment = total_new_comment - 1 WHERE id='.User::id().' AND total_new_comment>0');
		User::getUser(User::id(),0,1);
			
		$json.="})";
		echo $json;
		exit;
	}
	
	//Xem mạch lưu bút
	function show_user_comment_line(){
		$c_user_id  = (int)Url::get('c_user_id');
		$c_u_id  	= (int)Url::get('u_id');
		$group_id  	= (int)Url::get('group_id');
		
		$json = "";
		
		if(!User::is_login()){
			$json = '({"msg":"no_login"})';
			echo $json;
			exit;
		}
		if(User::is_block()){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit;
		}
		
		if($c_user_id && $c_u_id){
			$json = '({"msg":"success"';
			
			$all_user_comments 	= array();
			$all_users 			= array();
			
			$user = User::getUser($c_u_id);
			if($user){
				$re = DB::query("SELECT * FROM (
														(SELECT id, time, content, sender_user_id, sender_user_name FROM comment_user WHERE sender_user_id=".User::id()." AND receiver_user_id=$c_u_id AND id<$c_user_id ORDER BY id DESC LIMIT 20)
												UNION 
														(SELECT id, time, content, sender_user_id, sender_user_name FROM comment_user WHERE sender_user_id=$c_u_id AND receiver_user_id=".User::id()." AND id<$c_user_id ORDER BY id DESC LIMIT 20)
												)  AS u_comment
												ORDER BY id ASC LIMIT 20");
				
				$uids='';
				
				if($re){
					while ($u_comment = mysql_fetch_assoc($re)){	
						$u_comment['time']= AZLib::duration_time($u_comment['time']);
						$uids.=($uids?',':'').$u_comment['sender_user_id'];
						
						$all_user_comments[] = $u_comment;
					}
				}
				
				if($uids!=''){
					$re = DB::query("SELECT id, avatar_url, img_server FROM user WHERE id IN($uids)");
					if($re){
						while ($u_user = mysql_fetch_assoc($re)){
							//$all_users[$u_user['id']]=$u_user['avatar_url'];
							$all_users[$u_user['id']]=$u_user;
						}
					}
				}
				if($all_user_comments){
					$json.=',"c_user_line":"'.addslashes('<div style="padding:8px 6px;background-color:#ECEFF6;margin-bottom:2px;border-bottom:1px solid #ccc;font-size:11px">mạnh lưu bút với <a href="'.$user['user_name'].'" target="_blank"><font color="#3C5899"><b>'.$user['user_name'].'</b></font></a> - <span onclick="c_user_back('.$group_id.');" class="o_action">thu gọn</span></div>');	
					
					foreach ($all_user_comments as $u_comment){
						if(isset($all_users[$u_comment['sender_user_id']]['avatar_url']) && $all_users[$u_comment['sender_user_id']] && $all_users[$u_comment['sender_user_id']]['avatar_url']){
							$u_comment['avatar_url']='http://'.CGlobal::$img_server[$all_users[$u_comment['sender_user_id']]['img_server']].'thumb_wl/50/'.$all_users[$u_comment['sender_user_id']]['avatar_url'];
						}
						else{							
							$u_comment['avatar_url']='style/images/54x54.gif';
						}
						
				        $json.=addslashes(str_replace(array(chr(13),chr(10)),' ','
				        							<div class="o_sub_item">
								                            <div class="o_f_content">
								                            	<img src="'.$u_comment['avatar_url'].'" width="25" height="25" border="0" align="left" style="margin:0 6px 6px 0"/>
							                                	<a href="'.WEB_DIR.$u_comment['sender_user_name'].'" target="_blank">
										                            '.$u_comment['sender_user_name'].'
										                            </a> viết  <span class="o_time">'.$u_comment['time'].'</span><br />
										                            '.AZLib::parseBBCode($u_comment['content']).'
							                                </div>
							                                <div style="clear:left"><span></span></div>
					                               		</div>'));
				                    
					}
					
					$json.='"})';
				}
				else{
					$json.=',"c_user_line":""})';	
				}
				
				echo $json;
				exit;
			}
		}
		
		$json = "({'msg':'no_perm'})";
		echo $json;
		exit;
	}
	
	function action_user_comment(){
		$act_  		= Url::get('act_');
		
		$c_user_id 	= (int)Url::get('c_user_id',0);
		$content 	= trim(AZLib::getParam('content'));
		$json 		= "";
		
		if(!User::is_login()){
			$json = '({"msg":"no_login"})';
			echo $json;
			exit;
		}
		if(User::is_block()){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit;	
		}
		
		if($c_user_id){
			$comment_user = DB::select('comment_user',"id=$c_user_id");
		
			if($comment_user && $comment_user['receiver_user_id']==User::id()){
				
				if(($act_=='reply' && $content) || $act_=='skip'){
					$json = '({"msg":"success"';
					
					if($act_=='reply' && $content){
						if(AZLib::checkBadWord($content)){
							$json = '({"msg":"bad_word"})';
							echo $json;
							exit;	
						}
						
						$user = User::getUser($comment_user['sender_user_id']);
						
						if($user){
							$id=DB::insert('comment_user',array(
																'content'			=>$content,
																'time'				=>TIME_NOW,
																'post_ip'			=>AZLib::ip(),
																'sender_user_id'	=>User::id(),
																'sender_user_name'	=>User::user_name(),
																'receiver_user_id'	=>$user['id'],
																'receiver_user_name'=>$user['user_name'],
																'is_read'			=>0,						
																'status'			=>0						
																)
											);	
						
							if($id){
								$json .= ',"new_id":"'.$id.'"';
								
								DB::query('UPDATE user set total_comment_user=total_comment_user+1 WHERE id='.$user['id']);// so luu but
								User::getUser($user['id'],0,1);
								
								if($user['email'] && $user['email_alert'] && $user['id']!=User::id()){
									$link = WEB_ROOT.'?page=shop&user_name='.$user['user_name'].'&mode=comment';
									$link = AZRewrite::formatUrl($link);
									
									AZLib::addCronJob('user_comment',AZLib::parseBBCode($content,true),$user['id'],User::user_name(),'',0,'',$link);
								}
							}
						}
					}
					
					if($comment_user['status']==0){
						DB::query("UPDATE comment_user SET status=1, is_read=1 WHERE id=$c_user_id");
						//Cập nhật đã đọc - trả lời cho feed
						DB::query("UPDATE feed SET status = 1 WHERE ref_id = $c_user_id AND type=3");
						
						DB::query('UPDATE user set total_comment_user = total_comment_user-1 WHERE id='.User::id().' AND total_comment_user>0');// so luu but
						User::getUser(User::id(),0,1);
					}
					elseif ($act_=='skip'){
						//Cập nhật đã đọc - trả lời cho feed
						DB::query("UPDATE feed SET status = 1 WHERE ref_id = $c_user_id AND type=3");
					}
					
					$json.="})";
					echo $json;
					exit;
				}
				elseif($act_=='reply' && $content==''){
					$json = '({"msg":"short_content"})';
					echo $json;
					exit;
				}
			}
		}
		
		$json = "({'msg':'no_perm'})";
		echo $json;
		exit;
	}
	
	function action_pm(){
		$act_ 		= Url::get('act_');
		$pm_id 		= (int)Url::get('pm_id',0);
		$content 	= trim(AZLib::getParam('content'));
		$title_pm 	= '';
		$json 		= "";
		
		if(!User::is_login()){
			$json = '({"msg":"no_login"})';
			echo $json;
			exit;
		}
		if(User::is_block()){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit;
		}
		
		if($pm_id){
			$message_topic = DB::select('message_topics',"mt_id=$pm_id");
		
			if($message_topic && User::id()==$message_topic['mt_owner_id']){  	
				$message_text = DB::select('message_text',"msg_id={$message_topic['mt_msg_id']}");
				
				if(!$message_text){
					DB::delete('message_topics',"mt_id=$pm_id");
					
					$json = '({"msg":"no_perm"})';
					echo $json;
					exit;
				}
			}
			else{
				$json = '({"msg":"no_perm"})';
				echo $json;
				exit;
			}
			
			if(($act_=='reply' && $content) || $act_=='skip' || $act_=='delete'){
				$json = '({"msg":"success"';
				
				if($act_=='reply' && $content){
					if(AZLib::checkBadWord($content)){
						$json = '({"msg":"bad_word"})';
						echo $json;
						exit;
					}
					
					$title_pm = $message_topic['mt_title'];
					
					
					
					if($message_topic['mt_ref_id'])
					{
						$ref_id = $message_topic['mt_ref_id'];
					}
					else
					{
						$ref_id = $pm_id;
					}
					//if(strpos($title_pm,'Re:')!==0){
					//	$title_pm="Re: $title_pm";
					//}
					
					if(AZLib::isBlackList(User::id(),$message_topic['mt_from_id'])){
						if($message_topic['mt_read']==0)
						DB::query("UPDATE message_topics SET mt_read=1 WHERE mt_id=$pm_id");
						
						$json = '({"msg":"blacklist"})';
						echo $json;
						exit;
					}
					
					$user = User::getUser($message_topic['mt_from_id']);
					
					if($user){
						$id_mess = AZLib::Send_pm(User::id(), User::user_name(), $user,$title_pm,$content,0,$ref_id);																	
					
						if(!$id_mess){
							$json = '({"msg":"unsuccess"})';
							echo $json;
							exit;
						}
					}
					
					if($message_topic['mt_read']==0){
						DB::query("UPDATE message_topics SET mt_read=1 WHERE mt_id=$pm_id");
						//Cập nhật đã đọc - trả lời cho feed
						DB::query("UPDATE feed SET status = 1 WHERE ref_id = $pm_id AND type=4");
						
						DB::query('UPDATE user SET total_pm = total_pm-1 WHERE id='.User::id().' AND total_pm>0');
						User::getUser(User::id(),0,1);
					}
				}
				elseif($act_=='delete'){
					AZLib::delete_messages($pm_id);
					//Cập nhật đã đọc - trả lời cho feed
					DB::query("DELETE FROM feed WHERE ref_id = $pm_id AND type=4");
				}
				else{
					if($message_topic['mt_read']==0){
						DB::query("UPDATE message_topics SET mt_read=1 WHERE mt_id=$pm_id");
						DB::query('UPDATE user SET total_pm = total_pm-1 WHERE id='.User::id().' AND total_pm>0');
						User::getUser(User::id(),0,1);
					}
					
					//Cập nhật đã đọc - trả lời cho feed
					DB::query("UPDATE feed SET status = 1 WHERE ref_id = $pm_id AND type=4");
				}
				
				
				DB::query('UPDATE user SET total_pm = total_pm-1 WHERE id='.User::id().' AND total_pm>0');
				User::getUser(User::id(),0,1);
				$json.="})";
				echo $json;
				exit;
			}
			else{
				$json = '({"msg":"short_content"})';
				exit;
			}
		}
		
		$json = "({'msg':'no_perm'})";
		echo $json;
	}
	
	function action_entry_comment(){
		$act_	 	= Url::get('act_');
		$comment_id = (int)Url::get('c_entry_id',0);
		$content    = '';
		
		$json 		= "";
		
		if(!User::is_login()){
			$json = '({"msg":"no_login"})';
			echo $json;
			exit;
		}
		
		
		if(User::is_block() || !$comment_id || ($act_!='skip' && $act_!='reply')){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();			
		}
		
		$comment_row = DB::select('user_entry_comment',"id=$comment_id");
		if(!$comment_row){ 	
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();	
		}
		
		//if($comment_row['sender_user_id'] == User::id() ||  ($act_=='reply' && $comment_row['status']==1)){//Không được trả lời cho chính mình! hoặc trả lời phản hồi đã đc trả lời rồi!
		if($comment_row['sender_user_id'] == User::id()){//Không được trả lời cho chính mình!
			$json = '({"msg":"success"})';
			echo $json;
			exit();	
		}
		
		$entry_id  	= $comment_row['entry_id'];
		$entry		= DB::select('user_entry',"id=$entry_id");
		
		if(!($entry && $entry['user_id'] == User::id())){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();			
		}
	
		$json = '({"msg":"success"';
		
		if($act_=='reply'){
			$content    = trim(AZLib::getParam('content'));
			if(!$content){
				$json = '({"msg":"short_content"})';
				echo $json;
				exit();	
			}
			if(AZLib::checkBadWord($content)){
				$json = '({"msg":"bad_word"})';
				echo $json;
				exit();
			}
			
			$content 			= preg_replace( "/\n/", "<br />", $content);
			$content 			= str_replace('<br /><br />','&nbsp;',$content);
			
			if($comment_row['parent_id']){
				$parent_id		= $comment_row['parent_id'];
				
				if($comment_row['sender_user_id'])
					$content = '@<a href="'.WEB_DIR.$comment_row['sender_user_name'].'" class="fast_reply_link" title="'.$comment_row['sender_user_name'].'">'.$comment_row['sender_user_name'].'</a>: '.$content;
				else
					$content = '<font color="#999">@<span style="text-decoration:underline">'.$comment_row['sender_user_name'].'</span> </font>: '.$content;
			}
			else{
				$parent_id		= $comment_row['id'];
			}
			
			$comment = array(
							'time'				=>time(),
							'post_ip'			=>AZLib::ip(),
							'content'			=>$content,
							'entry_id'			=>$entry['id'],
							'order_time'		=>time(),
							'parent_id'			=>$parent_id,
							'receiver_user_id'	=>$entry['user_id'],
							'receiver_user_name'=>$entry['user_name'],
							'display'			=>1,
							'status'			=>1
							);
							
			$comment['sender_user_id']	=User::id();
			$comment['sender_user_name']=User::user_name();
			
			if($comment_row['sender_user_id']){
				$comment['replied_user_id']		= $comment_row['sender_user_id'];
				$comment['replied_user_name'] 	= $comment_row['sender_user_name'];
			}
			
			$id=DB::insert('user_entry_comment',$comment);
				
			if($id){
				$re = DB::query("SELECT id FROM user_entry_comment WHERE parent_id = $parent_id AND display = 1 ORDER BY id DESC LIMIT 3,1");				
				
				if($re){
					if($row = mysql_fetch_assoc($re)){
						if($row){
							DB::query("UPDATE user_entry_comment SET display = 0 WHERE parent_id = $parent_id AND display = 1 AND id<={$row['id']}");										
						}
					}
				}
				
				if($comment_id == $parent_id){
					DB::query("UPDATE user_entry_comment SET status = 1,have_child = have_child + 1, order_time = ".time()." WHERE id = $comment_id");
				}
				else{
					DB::query("UPDATE user_entry_comment SET have_child = have_child + 1, order_time = ".time()." WHERE id = $parent_id");
					DB::query("UPDATE user_entry_comment SET status = 1 WHERE id = $comment_id");
				}
				//Cập nhật đã đọc - trả lời cho feed
				DB::query("UPDATE feed SET status=1 WHERE ref_id = $comment_id AND type=5");
			}
			
			//Cập nhật cron job
			//...
		}
		else{
			if($comment_row['status']==0){
				DB::query("UPDATE user_entry_comment SET status = 1 WHERE id = $comment_id");
			}
			
			//Cập nhật đã đọc - trả lời cho feed
			DB::query("UPDATE feed SET status=1 WHERE ref_id = $comment_id AND type=5");
		}
		
		$json.="})";
		echo $json;
		exit;
		//System::halt();
	}
	
	function action_reply_entry_comment(){
		$act_	 	= Url::get('act_');
		$comment_id = (int)Url::get('re_c_entry_id',0);
		$content    = '';
		
		$json 		= "";
		
		if(!User::is_login()){
			$json = '({"msg":"no_login"})';
			echo $json;
			exit;
		}
		
		if(User::is_block() || !$comment_id || ($act_!='skip' && $act_!='reply')){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();			
		}
		
		$comment_row = DB::select('user_entry_comment',"id=$comment_id");
		
		if(!$comment_row || ($comment_row && $comment_row['replied_user_id'] != User::id())){ 	
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();	
		}
		
		if($comment_row['parent_id']==0 || $comment_row['receiver_user_id'] == User::id() || $comment_row['sender_user_id'] == User::id() ||  ($act_=='reply' && $comment_row['replied_status']==1)){//Không được trả lời cho chính mình! hoặc trả lời phản hồi đã đc trả lời rồi!
			$json = '({"msg":"success"})';
			echo $json;
			exit();	
		}
		
		$entry_id  	= $comment_row['entry_id'];
		$entry		= DB::select('user_entry',"id=$entry_id");
		
		if(!$entry || ($entry && $entry['status']==0)){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();			
		}
	
		$json = '({"msg":"success"';
		
		if($act_=='reply'){
			$content    = trim(AZLib::getParam('content'));
			if(!$content){
				$json = '({"msg":"short_content"})';
				echo $json;
				exit();	
			}
			if(AZLib::checkBadWord($content)){
				$json = '({"msg":"bad_word"})';
				echo $json;
				exit();
			}
			
			$content 			= preg_replace( "/\n/", "<br />", $content);
			$content 			= str_replace('<br /><br />','&nbsp;',$content);
			
			$parent_id		= $comment_row['parent_id'];
			
			if($comment_row['sender_user_id'])
				$content = '@<a href="'.WEB_DIR.$comment_row['sender_user_name'].'" class="fast_reply_link" title="'.$comment_row['sender_user_name'].'">'.$comment_row['sender_user_name'].'</a>: '.$content;
			else
				$content = '<font color="#999">@<span style="text-decoration:underline">'.$comment_row['sender_user_name'].'</span> </font>: '.$content;
			
			$comment = array(
							'time'				=>time(),
							'post_ip'			=>AZLib::ip(),
							'content'			=>$content,
							'entry_id'			=>$entry['id'],
							'order_time'		=>time(),
							'parent_id'			=>$parent_id,
							'receiver_user_id'	=>$entry['user_id'],
							'receiver_user_name'=>$entry['user_name'],
							'display'			=>1,
							'status'			=>1
							);
							
			$comment['sender_user_id']	=User::id();
			$comment['sender_user_name']=User::user_name();
			
			if($comment_row['sender_user_id']){
				$comment['replied_user_id']		=$comment_row['sender_user_id'];
				$comment['replied_user_name'] 	=$comment_row['sender_user_name'];
			}
			
			$id=DB::insert('user_entry_comment',$comment);
				
			if($id){
				$re = DB::query("SELECT id FROM user_entry_comment WHERE parent_id = $parent_id AND display = 1 ORDER BY id DESC LIMIT 3,1");				
				
				if($re){
					if($row = mysql_fetch_assoc($re)){
						if($row){
							DB::query("UPDATE user_entry_comment SET display = 0 WHERE parent_id = $parent_id AND display = 1 AND id<={$row['id']}");										
						}
					}
				}
				
				if($comment_id == $parent_id){
					DB::query("UPDATE user_entry_comment SET replied_status = 1,have_child = have_child + 1, order_time = ".time()." WHERE id = $comment_id");
				}
				else{
					DB::query("UPDATE user_entry_comment SET have_child = have_child + 1, order_time = ".time()." WHERE id = $parent_id");
					DB::query("UPDATE user_entry_comment SET replied_status = 1 WHERE id = $comment_id");
				}
				//Cập nhật đã đọc - trả lời cho feed
				DB::query("UPDATE feed SET status=1 WHERE ref_id = $comment_id AND type=6");
			}
			
			//Cập nhật cron job
			$user_item = User::getUser($entry['user_id']);
			if($user_item['email'] && $user_item['email_alert'] && $user_item['id']!=User::id()){			
					
				$link = WEB_ROOT.'?page=user_entry&user_name='.$entry['user_name'].'&cmd_entry=view&entry_id='.$entry['id'].'&ebname='.AZLib::safe_title($entry['title']);
				$link = AZRewrite::formatUrl($link);
				$title = "<a href='$link' target='_blank' style='text-decoration:none;color:#003399;'><font color='#003399'>{$entry['title']}</font></a>";
					  
				AZLib::addCronJob('entry_comment',AZLib::parseBBCode($content,true),$user_item['id'],User::user_name(),'',$entry['id'],$title,$link);
			}
		}
		else{
			if($comment_row['replied_status']==0){
				DB::query("UPDATE user_entry_comment SET replied_status = 1 WHERE id = $comment_id");
			}
			
			//Cập nhật đã đọc - trả lời cho feed
			DB::query("UPDATE feed SET status=1 WHERE ref_id = $comment_id AND type=6");
		}
		
		$json.="})";
		echo $json;
		exit;
	}
	
	function view_more_o_trans(){
		$page_no 	= (int)Url::get('page_no',0);
		$cur_group 	= (int)Url::get('cur_total_group',0);
		$action 	= Url::get('get_act');
		
		if(!User::is_login()){
			$json = '({"msg":"no_login"})';
			echo $json;
			exit;
		}
		if(User::is_block()){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit;
		}
		
		if($page_no>0){
			$json = '({"msg":"success"';
			
			require_once(DIR_MODULE."Personal/forms/Office.php");
			$OfficeForm 		= new OfficeForm("OfficeForm");
			$transact_office 	= $OfficeForm->draw();

			$json .= ',"content":"'.addslashes(str_replace(array(chr(13),chr(10)),' ',$transact_office)).'"';
			$json .= ',"total_group":"'.$OfficeForm->total_group.'","show_more":"'.$OfficeForm->show_more.'"';
			$json .= '})';
			
			echo $json;
			System::halt();
		}
		
		$json = '({"msg":"no_perm"})';
		echo $json;
		exit;
	}
}//class
?>